<template>
    <n-drawer v-model:show="isDrawer" width="650" :auto-focus="false" @after-leave="handleReset">
      <n-drawer-content title="提现审核" closable>
        <n-form
          :model="formParams"
          ref="formRef"
          label-placement="left"
          :label-width="100"
        >
          <n-form-item label="交易单号" path="danhao">
            <n-input
              placeholder=""
              v-model:value="formParams.danhao"
              disabled
            />
          </n-form-item>
          <n-form-item label="申请时间" path="create_time">
            <n-input
              placeholder=""
              v-model:value="formParams.create_time"
              disabled
            />
          </n-form-item>
          <n-form-item label="用户名" path="username">
            <n-input
              placeholder=""
              v-model:value="formParams.username"
              disabled
            />
          </n-form-item>
          <n-form-item label="手机号" path="tell">
            <n-input
              placeholder=""
              v-model:value="formParams.tell"
              disabled
            />
          </n-form-item>
          <n-form-item label="提现金额" path="yuanmoney">
            <n-input
              placeholder=""
              v-model:value="formParams.yuanmoney"
              disabled
            />
          </n-form-item>
          <n-form-item label="手续费" path="shouxufei">
            <n-input
              placeholder=""
              v-model:value="formParams.shouxufei"
              disabled
            />
          </n-form-item>
          <n-form-item label="实际打款" path="money">
            <n-input
              placeholder=""
              v-model:value="formParams.money"
              disabled
            />
          </n-form-item>
          <n-form-item label="状态" path="status">
            <n-radio-group v-model:value="formParams.status">
              <n-space>
                <n-radio :key="0" :value="0">待审核</n-radio>
                <n-radio :key="1" :value="1">通过审核</n-radio>
              </n-space>
            </n-radio-group>
          </n-form-item>
        </n-form>
        <template #footer>
          <n-space>
            <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
          </n-space>
        </template>
      </n-drawer-content>
    </n-drawer>
  </template>
  
  <script lang="ts" setup>
    import { ref } from 'vue';
    import { useMessage } from 'naive-ui';
    import { cashInfo,cashshenhe,cashagentshenhe,alipaycashshenhe } from '@/api/financial/index';

    const defaultValueRef = () => ({
      id: null,
      danhao: '',
      create_time: '',
      users_id: '',
      agent_id: '',
      username: '',
      tell: '',
      yuanmoney: '',
      shouxufei: '',
      money: '',
      status: 0,
      role_id: '',
      pingtai: 1,
      name: '',
      id_card: '',
    });
  
    const message = useMessage();
    const formRef: any = ref(null);
    const isDrawer = ref(false);
    const subLoading = ref(false);
    const formParams = ref(defaultValueRef());
    const emit = defineEmits(['change']);

    async function openDrawer(id?) {
      if (id) {
        formParams.value.id = id;
        getInfo();
        return;
      }
      isDrawer.value = true;
    }
  
    function closeDrawer() {
      isDrawer.value = false;
    }
  
    function formSubmit() {
      formRef.value.validate((errors) => {
        if (!errors) {
          const msg = formParams.value.id ? '编辑成功' : '添加成功';
          if (formParams.value.id) {
            if(formParams.value.role_id == '10'){
              if(formParams.value.pingtai == 3){
                alipaycashshenhe(formParams.value).then((_) => {
                  message.success(msg);
                  emit('change');
                  handleReset();
                  closeDrawer();
                });
              }else{
                cashshenhe(formParams.value).then((_) => {
                  message.success(msg);
                  emit('change');
                  handleReset();
                  closeDrawer();
                });
              }
            }else{
              cashagentshenhe(formParams.value).then((_) => {
                message.success(msg);
                emit('change');
                handleReset();
                closeDrawer();
              });
            }
          }
        } else {
          message.error('请填写完整信息');
        }
      });
    }
  
    function handleReset() {
      formRef.value.restoreValidation();
      formParams.value = Object.assign(formParams.value, defaultValueRef());
    }
  
    function getInfo() {
      cashInfo({ id: formParams.value.id }).then((res) => {
        const params = {
          id: formParams.value.id,
          danhao: res.danhao,
          create_time: res.create_time,
          users_id: res.users_id,
          agent_id: res.agent_id,
          username: res.username,
          tell: res.tell,
          yuanmoney: res.yuanmoney,
          shouxufei: res.shouxufei,
          money: res.money,
          status: res.status,
          role_id: res.role_id,
          pingtai: res.pingtai,
          name: res.name,
          id_card: res.id_card,
        };
        formParams.value = Object.assign(formParams.value, params);
        isDrawer.value = true;
      });
    }
  
    defineExpose({
      openDrawer,
      closeDrawer,
    });
  </script>
  